import {Component, OnInit} from '@angular/core';
import {FetchService} from "../../../../core/net/fetch.service";
import {format} from 'date-fns';
import {DomSanitizer, SafeResourceUrl} from "@angular/platform-browser";

@Component({
  selector: 'app-report-hhsq',
  templateUrl: './hhsq.component.html',
  styleUrls: ['./hhsq.component.scss', '../report.component.scss']
})
export class HhsqComponent implements OnInit {
  listData = [];
  contentTitle: string;
  yearValue = 2020;
  monthPickerValue = new Date().getMonth();
  selectedItem;
  serverUrl = (window as any).environment.reportUrl;
  iframeSrc: SafeResourceUrl;


  constructor(
    private fetchService: FetchService,
    private sanitizer: DomSanitizer
  ) {
  }

  ngOnInit() {
    this.iframeSrc = this.sanitizer.bypassSecurityTrustResourceUrl('');
    this.queryLeftList();
  }

  selectedAreaChanged(item): void {
    this.selectedItem = item;
    const reportName = this.selectedItem.stadiaType == '7' ? '水情报表' : '水库报表';
    this.contentTitle = this.selectedItem.stadiaName + (this.monthPickerValue + 1) + '月' + reportName;
    this.queryInfo();
  }


  queryLeftList(): void {
    this.fetchService.post<any>('wrs/wrsBrstadia/list', {}).subscribe(res => {
      this.listData = res.list;
      this.selectedItem = this.listData[0];
      this.selectedAreaChanged(this.selectedItem);
    });
  }

  queryInfo(): void {
    const year = this.yearValue;
    const month = this.monthPickerValue + 1;
    const reportName = this.selectedItem.stadiaType == '7' ? '黄河水情河道水情报表' : '黄河水情水库报表';
    const stadiaCode = this.selectedItem.stadiaCode;
    const token = localStorage.token;
    const link = `${this.serverUrl}preview?_u=wrs:${reportName}.ureport.xml&_t=3,6&yearStr=${year}&monthStr=${month}&stadiaCode=${stadiaCode}&token=${token}`;
    this.iframeSrc = this.sanitizer.bypassSecurityTrustResourceUrl(link);
  }

  dateChanged() {
    const reportName = this.selectedItem.stadiaType == '7' ? '水情报表' : '水库报表';
    this.contentTitle = this.selectedItem.stadiaName + (this.monthPickerValue + 1) + '月' + reportName;
    this.queryInfo();
  }
}

